package com.cheer.controller;

import com.cheer.enums.TrueOrFalseEnum;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.ApplicantService;
import com.cheer.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/register")
public class RegisterReviewController {

    @Autowired
    private ApplicantService applicantService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    /**
     * 分页查询注册审核集体
     */
    @ApiOperation(" 增加 isSubmitAgain 参数 为 1 的时查询驳回后提交的 ")
    @GetMapping("/list")
    public Result<Page<ApplicantRegisterVO>> registerReview(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                            @RequestParam(required = false) Long orgId,
                                                            @RequestParam(required = false) String applicantName,
                                                            @RequestParam(required = false) String contactPerson,
                                                            @RequestParam(required = false) Integer status,
                                                            @RequestParam(required = false) Integer applicantStatus,
                                                            @RequestParam(required = false) Boolean submitted,
                                                            @RequestParam(required = false) Integer reviewType,
                                                            @RequestParam(required = false) Integer registerStatus,
                                                            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                                                            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                                                            @RequestParam(required = false) Integer isSubmitAgain) {

        if(TrueOrFalseEnum.TRUE.getType().equals(isSubmitAgain)){
            Page<ApplicantRegisterVO> page = applicantService.createSubmitAgain(pageNum, pageSize,  userInfoHolder.getAdminOrgId());
            return Result.success(page);
        }

        if (orgId == null) {
            orgId = userInfoHolder.getAdminOrgId();
        }
        Page<ApplicantRegisterVO> page = applicantService.getReviewPage(pageNum, pageSize, orgId, applicantName,
                contactPerson, status, applicantStatus, submitted, reviewType, registerStatus, startTime, endTime);
        return Result.success(page);
    }


    @ApiOperation(value = "已驳回再次提交数量")
    @GetMapping("/submit/again/number")
    public Result<Integer> submitAgainNumber() {

        Integer submitAgainNumber = applicantService.createSubmitAgainNumber(userInfoHolder.getAdminOrgId());
        return Result.success(submitAgainNumber);
    }

    /**
     * 导出注册审核集体
     */
    @GetMapping("/download")
    public void download(@RequestParam(required = false) Long orgId,
                         @RequestParam(required = false) String applicantName,
                         @RequestParam(required = false) String contactPerson,
                         @RequestParam(required = false) Integer status,
                         @RequestParam(required = false) Integer applicantStatus,
                         @RequestParam(required = false) Boolean submitted,
                         @RequestParam(required = false) Integer reviewType,
                         @RequestParam(required = false) Integer registerStatus,
                         @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                         @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        if (orgId == null) {
            orgId = userInfoHolder.getAdminOrgId();
        }
        applicantService.downloadReview(orgId, applicantName, contactPerson, status, applicantStatus, submitted,
                reviewType, registerStatus, startTime, endTime);
    }

    /**
     * 通过注册审核
     */
    @GetMapping("/pass")
    public Result pass(Long applicantId) {
        applicantService.approveRegister(applicantId, userInfoHolder.getAccountId(), userInfoHolder.getAdminOrgId());
        return Result.success();
    }

    /**
     * 批量通过注册审核
     */
    @PostMapping("/pass/batch")
    public Result passAll(@RequestBody List<Long> applicantIds) {
        applicantService.batchApproveRegister(applicantIds, userInfoHolder.getAccountId(), userInfoHolder.getAdminOrgId());
        return Result.success();
    }

    /**
     * 不通过注册审核
     */
    @GetMapping("/deny")
    public Result deny(Long applicantId, String cause) {
        applicantService.denyRegister(applicantId, userInfoHolder.getAccountId(), userInfoHolder.getAdminOrgId(), cause);
        return Result.success();
    }

    /**
     * 批量不通过注册审核
     */
    @PostMapping("/deny/batch")
    public Result denyAll(@RequestBody List<Long> applicantIds) {
        applicantService.batchDenyRegister(applicantIds, userInfoHolder.getAccountId(), userInfoHolder.getAdminOrgId());
        return Result.success();
    }

    /**
     * 查询集体详情
     */
    @GetMapping("/info")
    public Result info(Long applicantId) {
        ApplicantVO applicantVO = applicantService.getByApplicantId(applicantId);
        return Result.success(applicantVO);
    }

    /**
     * 查看日志
     */
    @GetMapping("/log")
    public Result<List<StandingBookReviewLogVO>> getLog( @RequestParam Long applicantId,
                                                         @RequestParam(required = false) Integer type ) {
        List<StandingBookReviewLogVO> list = applicantService.getLog(applicantId, type);
        return Result.success(list);
    }
}
